//
//  BaseDetailsController.m
//  网易新闻
//
//  Created by dllo on 15/10/7.
//  Copyright (c) 2015年 Lx. All rights reserved.
//

#import "BaseDetailsController.h"
#import "SDWebImage/UIImageView+WebCache.h"
#import "Settings.h"
@interface BaseDetailsController ()<UIWebViewDelegate>
@property(nonatomic, retain)UIScrollView *scroll;
@property(nonatomic, assign)float scroll_height;
@property(nonatomic, retain)UIWebView *web;
@property(nonatomic, retain)UILabel *titlelab;
@property(nonatomic, retain)UILabel *namelab;
@end
@implementation BaseDetailsController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [[Settings shareSettings]color:WhiteToGray];
    self.scroll_height = 25;
    [self handData];
    [self notificationHandle];
}

- (void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBar.barTintColor = [[Settings shareSettings]color:WhiteToGray];
}

- (void)createTitle{
    self.titlelab = [[UILabel alloc]initWithFrame:CGRectMake(15, self.scroll_height, 345, 25)];
    self.titlelab.textColor = [[Settings shareSettings]color:BlackToWhite];
    self.titlelab.font = [UIFont systemFontOfSize:22];
    self.titlelab.text = self.mod.title;
    [self.scroll addSubview:self.titlelab];
    [self.titlelab release];
    
    self.namelab = [[UILabel alloc]initWithFrame:CGRectMake(15, self.scroll_height+25, 210, 25)];
    self.namelab.textColor = [[Settings shareSettings]color:BlackToWhite];
    self.namelab.font = [UIFont systemFontOfSize:11];
    self.namelab.text = [NSString stringWithFormat:@"%@ %@", self.mod.source, self.mod.ptime];
    [self.scroll addSubview:self.namelab];
    [self.namelab release];
    
    self.scroll_height += 50;
    self.scroll.contentSize = CGSizeMake(375, self.scroll_height);
}

- (void)addImg{
    UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(15, self.scroll_height+25, 345, 255)];
    img.backgroundColor = [UIColor whiteColor];
    img.contentMode = UIViewContentModeScaleAspectFit;
    NSURL *url = [NSURL URLWithString:[[self.mod.img firstObject]objectForKey:@"src"]];
    if (url != nil) {
        [img sd_setImageWithURL: url placeholderImage:nil];
        [url release];
        [self.scroll addSubview:img];
        [img release];
        self.scroll_height += 255;
        self.scroll.contentSize = CGSizeMake(375, self.scroll_height);
    }
}

- (void)createWebView{
    self.web = [[UIWebView alloc]init];
    self.web.scrollView.bounces = NO;
    self.web.delegate = self;
    self.web.frame = CGRectMake(0, self.scroll_height, 375, 1);
    
    NSString *night = @"";
    if ([Settings shareSettings].isNight) {
        night = @"bgcolor=\"#4D4D4D\" text=\"white\" ";
    }else{
        night = @"bgcolor=\"#ffffff\" text=\"black\" ";
    }
    [self.web loadHTMLString:[NSString stringWithFormat:@"\
                              <!DOCTYPE HTML>\
                              <html>\
                              <body %@ >\
                              %@\
                              </body> </html>",night, self.mod.body] baseURL:nil];
    [self.scroll addSubview:self.web];
    [self.web release];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView{
    CGRect frame = webView.frame;
    CGSize fittingSize = [webView sizeThatFits:CGSizeZero];
    frame.size = fittingSize;
    webView.frame = frame;
    self.scroll_height += webView.frame.size.height;
    self.scroll.contentSize = CGSizeMake(375, self.scroll_height);
}

- (void)createScrollView{
    self.scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 60, 375, 620)];
    self.scroll.backgroundColor = [[Settings shareSettings]color:WhiteToGray];
    self.scroll.bounces = NO;
    self.scroll.showsHorizontalScrollIndicator = NO;
    self.scroll.contentSize = CGSizeMake(375, self.scroll_height);
    
    [self.view addSubview:self.scroll];
    [self.scroll release];
}

- (void)handData{

}

- (void)createSelf{
    [self createScrollView];
    [self createTitle];
    [self addImg];
    [self createWebView];
}

- (void)notificationHandle{
    [[NSNotificationCenter defaultCenter]addObserverForName:@"changeBackgroundColor" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {
        self.view.backgroundColor = [[Settings shareSettings]color:WhiteToGray];
        self.scroll.backgroundColor = [[Settings shareSettings]color:WhiteToGray];
        self.namelab.textColor = [[Settings shareSettings]color:BlackToWhite];
        self.titlelab.textColor = [[Settings shareSettings]color:BlackToWhite];
        self.navigationController.navigationBar.barTintColor = [[Settings shareSettings]color:WhiteToGray];
        
        NSString *night = @"";
        if ([Settings shareSettings].isNight) {
            night = @"bgcolor=\"#4D4D4D\" text=\"white\" ";
        }else{
            night = @"bgcolor=\"#ffffff\" text=\"black\" ";
        }
        [self.web loadHTMLString:[NSString stringWithFormat:@"\
                                  <!DOCTYPE HTML>\
                                  <html>\
                                  <body %@ >\
                                  %@\
                                  </body> </html>",night, self.mod.body] baseURL:nil];
        self.scroll_height -= self.web.frame.size.height;
        self.scroll.contentSize = CGSizeMake(375, self.scroll_height);
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)dealloc{
    [_titlelab release];
    [_namelab release];
    [_docid release];
    [_mod release];
    [_web release];
    [super dealloc];
}


@end

